package main

import "fmt"

func exchange(nums []int) []int {
	//  双指针，前后遍历
	start, end := 0, len(nums)-1
	for start < end {
		// 正向遍历nums直到nums[start]为偶数，或者start>end
		for start < end && nums[start]%2 == 1 {
			start++
		}
		// 逆向遍历nums直到nums[end]]为奇数，或者start>end
		for start < end && nums[end]%2 == 0 {
			end--
		}
		nums[start], nums[end] = nums[end], nums[start]
	}
	return nums
}

func main() {
	fmt.Println(exchange([]int{1,2,3,4}))
}
